/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.rate.event.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.enums.rate.AuditStatusEnum;
import org.springblade.common.enums.rate.EventTypeEnum;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.auth.utils.TokenUtil;
import org.springblade.modules.rate.company.entity.CompanyTarget;
import org.springblade.modules.rate.company.service.ICompanyTargetService;
import org.springblade.modules.rate.event.vo.audit.EventAuditReqVO;
import org.springblade.modules.rate.event.vo.audit.EventAuditOperationVO;
import org.springblade.modules.rate.event.vo.detail.EventDetailVO;
import org.springblade.modules.rate.event.vo.detail.EventNoticeVO;
import org.springblade.modules.rate.event.vo.dismiss.EventDismissVO;
import org.springblade.modules.rate.event.vo.like.EventLikeOperationVO;
import org.springblade.modules.system.entity.Dict;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDictService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Collections;
import java.util.List;
import org.springblade.modules.rate.event.entity.Event;
import org.springblade.modules.rate.event.vo.EventVO;
import org.springblade.modules.rate.event.service.IEventService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 事件表 控制器
 *
 * @author Blade
 * @since 2021-09-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("rate/event/event")
@Api(value = "事件表", tags = "事件动态接口")
public class EventController extends BladeController {

	private IEventService eventService;
	private IDictService dictService;
	private IUserService userService;
	private ICompanyTargetService companyTargetService;


	//小程序

	@PostMapping("/publish")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】发布事件", notes = "传入event")
	public R publish(@Valid @RequestBody EventVO event) {
		return R.status(eventService.publish(event));
	}

	@PostMapping("/pickLike")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】事件加分", notes = "传入event")
	public R pickLike(@Valid @RequestBody EventLikeOperationVO event) {
		return R.status(eventService.pickLike(event));
	}

	@PostMapping("/dismiss")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】解封事件", notes = "传入event")
	public R dismiss(@Valid @RequestBody EventDismissVO dismissVO) {
		return R.status(eventService.dismiss(dismissVO));
	}
	@GetMapping("/getCommitEvent")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】获取提交中事件-分页")
	public R<IPage<Event>> getCommitEvent(Query query,BladeUser bladeUser) {
		IPage<Event> pages =  eventService.getCommitEvent(Condition.getPage(query),bladeUser);
		return R.data(pages);
	}


	@GetMapping("/getUnAuditEvvent")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】获取未审核事件", notes = "传入event")

	public R<IPage<Event>> getUnAuditEvvent(EventAuditReqVO auditReqVO, Query query) {
		IPage<Event> pages = eventService.getUnAuditEvvent(Condition.getPage(query),auditReqVO);
		return R.data(pages);
	}

	@PostMapping("/audit")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】审核事件（1.审核通过 2.审核拒绝）", notes = "传入event")
	public R audit(@Valid @RequestBody EventAuditOperationVO eventAuditOperationVO) {
		return R.status(eventService.audit(eventAuditOperationVO));
	}

	@GetMapping("/getEventType")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】获取事件类型")
	public R<List<Dict>> getEventType() {
		Dict dict = new Dict();
		dict.setCode("rate_EventType");
		List<Dict> items = dictService.list(Condition.getQueryWrapper(dict));
		return R.data(items);
	}

	@GetMapping("/getCompanyTarget")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】公司目标大纲")
	public R<List<CompanyTarget>> getCompanyTarget(BladeUser bladeUser) {
		User user = userService.getById(bladeUser.getUserId());
		CompanyTarget companyTarget = new CompanyTarget();
		companyTarget.setTenantId(user.getTenantId());
		List<CompanyTarget> items = companyTargetService.list(Condition.getQueryWrapper(companyTarget));
		if (CollectionUtil.isEmpty(items)) {
			companyTarget.setTenantId(TokenUtil.DEFAULT_TENANT_ID);
			items = companyTargetService.list(Condition.getQueryWrapper(companyTarget));
		}
		return R.data(items);
	}

	@GetMapping("/getMemberList")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】获取公司人员列表")
	public R<List<User>> getMemberList() {
		User user = new User();
		user.setIsDeleted(0);
		List<User> items = userService.list(Condition.getQueryWrapper(user));
		return R.data(items);
	}
	@GetMapping("/getDetail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】获取动态详情")
	public R<EventDetailVO> getDetail(Event event) {
		EventDetailVO detail = eventService.getDetail(event);
		return R.data(detail);
	}

	@GetMapping("/getEventNotice")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】获取事件相关数量")
	public R<EventNoticeVO> getEventNotice() {
		EventNoticeVO notice = eventService.getEventNotice();
		return R.data(notice);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "详情", notes = "传入event")
	public R<Event> detail(Event event) {
		Event detail = eventService.getOne(Condition.getQueryWrapper(event));
		return R.data(detail);
	}

	/**
	 * 分页 事件表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表查询", notes = "传入event")
	public R<IPage<Event>> list(Event event, Query query) {
		IPage<Event> pages = eventService.page(Condition.getPage(query), Condition.getQueryWrapper(event));
		return R.data(pages);
	}

	/**
	 * 自定义分页 事件表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】分页", notes = "传入event")
	public R<IPage<Event>> page(EventVO event, Query query) {
		query.setDescs("update_time");
		IPage<Event> pages = eventService.getEventPage(Condition.getPage(query), event);
		return R.data(pages);
	}

	/**
	 * 新增 事件表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入event")
	public R save(@Valid @RequestBody Event event) {
		return R.status(eventService.save(event));
	}

	/**
	 * 修改 事件表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入event")
	public R update(@Valid @RequestBody Event event) {
		return R.status(eventService.updateById(event));
	}

	/**
	 * 新增或修改 事件表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入event")
	public R submit(@Valid @RequestBody Event event) {
		return R.status(eventService.saveOrUpdate(event));
	}


	/**
	 * 删除 事件表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(eventService.removeByIds(Func.toLongList(ids)));
	}

	    /**
     * 数组 代码自定义代号
     */
    @GetMapping("/item")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "全部列表", notes = "传入event")
    public R<List<Event>> list(Event event) {
        List<Event> items = eventService.list(Condition.getQueryWrapper(event));
        return R.data(items);
    }


}
